package org.catlabs.patterns.observer.subject;

import java.util.ArrayList;

import org.catlabs.patterns.observer.observer.Observer;


public class WeatherData implements Subject {

    private ArrayList<Observer> observers;
    private float temperature;
    private float humidity;
    private float pressure;
    
    public WeatherData(){
	observers = new ArrayList<Observer>();
    }
    
    public float getTemperature(){
	return temperature;
    }
    
    public float getHumidity(){
	return humidity;
    }
    
    public float getPressure(){
	return pressure;
    }
    
    /**
     * Class state is affected from other classes through this method.
     * @param temp
     * @param humidity
     * @param pressure
     */
    public void setMeasurements(float temp, float humidity, float pressure){
	this.temperature = temp;
	this.humidity = humidity;
	this.pressure = pressure;
	
	measurementsChanged();
    }
    
    /**
     * This is what the class does after the object state is affected.
     */
    public void measurementsChanged() {
	notifyObservers();
    }

    @Override
    public void registerObserver(Observer o) {
	observers.add(o);
    }

    @Override
    public void removeObserver(Observer o) {
	int i = observers.indexOf(o); // returns the position of the object (if any)
	if(i != -1)
	    observers.remove(i);
    }

    @Override
    public void notifyObservers() {
	for(Observer observer : observers){
	    observer.update(temperature, humidity, pressure);
	}
    }
    
}
